﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace HPITSSBLL
{
  public  class DBHelper
    {
        public static string Constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="type"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql,params SqlParameter[] pams )
        {
            //1:声明数据库连接
            SqlConnection conn = new SqlConnection(Constr);
            //2:声明命令
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                //添加参数
                if(pams!=null&&pams.Length>0)
                {
                    cmd.Parameters.AddRange(pams);
                }
                //3:打开数据库连接
                if(conn.State==ConnectionState.Closed)
                {
                    conn.Open();
                }
                
                //4:执行命令
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }

        internal static SqlDataReader ExecuteReader(string sql, object pams)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql,params SqlParameter[] pams)
        {
            //1:声明数据库连接
            using(SqlConnection conn=new SqlConnection (Constr ))
            {
                //2:声明命令
                using(SqlCommand cmd=new SqlCommand (sql,conn))
                {
                    //添加参数
                    if(pams!=null&&pams.Length>0)
                    {
                        cmd.Parameters.AddRange(pams);
                    }
                    //3:打开数据库连接
                    if(conn.State==ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    //4:执行命令
                    return cmd.ExecuteNonQuery();
                }
            }
        }
    }
}
